<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">

    <style>
        .mainContect img {
        }
    </style>
    <!--<script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&key=VLSBZ-4N5CP-MNGDU-VMGRP-ZWUZ5-QNFH4"></script>-->


    <!--<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=MGzgh01XXLpPwuk2nsxFv0Y1wI9QBbmY"></script>-->
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=1yGIc901IHvG65b3jGUu5nrkWUOYpT3c"></script>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>首页
            <small>公司分布图</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>首页</el-breadcrumb-item>
            <el-breadcrumb-item>公司分布图</el-breadcrumb-item>
        </el-breadcrumb>
    </div>


    <template>
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
            <el-tab-pane label="分布图" name="first">
                <div id="bigMap" style="width:1400px;height:600px;"></div>


            </el-tab-pane>
            <el-tab-pane label="详细信息" name="second">
                <div class="filter-container">
                    <el-input placeholder="公司地址" v-model="pagination.queryString" style="width: 200px;"
                              class="filter-item"></el-input>
                    <el-button @click="findPage()" class="dalfBut">查询</el-button>
                    <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                </div>
                <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                    <el-table-column type="index" align="center" label="序号"></el-table-column>
                    <el-table-column prop="address" label="公司地址" align="center"></el-table-column>
                    <el-table-column prop="longitude" label="所在经度" align="center"></el-table-column>
                    <el-table-column prop="latitude" label="所在纬度" align="center"></el-table-column>

                    <el-table-column label="操作" align="center">
                        <template slot-scope="scope">
                            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination-container">
                    <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                    </el-pagination>
                </div>

                <!-- 新增 -->
                <div class="add-form">
                    <el-dialog title="新增地址" :visible.sync="dialogFormVisible" @opened="createMap()">
                        <el-form ref="dataAddForm" :model="formData" label-position="right" label-width="100px">

                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="地址名" prop="name">
                                        <div id="r-result" >请输入:<input type="text" id="suggestId" v-model="formData.address" @blur="parseAddress(formData.address)" size="20" value="百度" style="width:150px;" /></div>
                                        <div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-button size="mini" type="info" >查看地址</el-button>
                                </el-col>
                                <el-col :span="6">
                                    <el-button size="mini" type="success" @click="handleAdd()">确定添加</el-button>
                                </el-col>


                            </el-row>

                        </el-form>
                        <div id="smallMap" style="width:95%;height:500px;" ></div>

                        <div slot="footer" class="dialog-footer">
                            <el-button @click="dialogFormVisible = false">取消</el-button>
                            <el-button type="primary" @click="handleAdd()">确定</el-button>
                        </div>

                    </el-dialog>


                </div>
            </el-tab-pane>

        </el-tabs>
    </template>


</div>


</body>


<script type="text/javascript">

</script>

<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>

<!--<script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&key=VLSBZ-4N5CP-MNGDU-VMGRP-ZWUZ5-QNFH4"></script>-->


<script>

    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 3,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            // dataList: [],
            activeName: 'first',
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见

            dataList: [//当前页要展示的分页列表数据

            ],

        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage()
        },
        mounted() {




            var map = new BMap.Map("bigMap");
            //// 创建地图实例
            var point = new BMap.Point(114.309043,30.550317);
            //// 创建点坐标
            map.centerAndZoom(point, 12);
            map.enableScrollWheelZoom();
            window.map=map;
            this.mapAddress();

            //##############################################

        },
        methods: {

            //添加
            handleAdd() {

                var param={
                    address:this.formData.address,
                    longitude:lng,
                    latitude:lat,
                }
                axios.post("/address/add.do",param).then((resp)=>{
                    this.dialogFormVisible = false;
                    if(resp.data.flag){
                        this.$message.success(resp.data.message)
                        this.findPage();
                    }else {
                        this.$message.error(resp.data.message);
                    }

                    location.reload();
                })


            },
            //分页查询
            findPage() {
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: $.trim(this.pagination.queryString)

                }
                axios.post("/address/findPage.do", param).then((res) => {

                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows
                }).catch((err) => {
                    console.log(err)
                })

            },
            findAll() {

                axios.post("/address/findAll.do").then((res) => {

                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows
                }).catch((err) => {
                    console.log(err)
                })

            },
            // 重置表单
            resetForm() {
                this.formData = {}//重置数据
            },
            // 弹出添加窗口
            handleCreate() {
                this.resetForm()
                this.dialogFormVisible = true;


            },

            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage()
            },

            //删除检查项 权限不够的报错处理方法
            showMessage(r) {
                if (r == 'Error: Request failed with status code 403') {
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                } else {
                    this.$message.error('未知错误');
                    return;
                }
            },
            // 删除
            handleDelete(row) {//row其实是一个json对象，json对象的结构为{"age":"0-100","attention":"无","code":"0011","id":38,"name":"白细胞计数","price":10.0,"remark":"白细胞计数","sex":"0","type":"2"}
                this.$confirm("你确定删除吗？", "提示", {
                    type: "warning"
                }).then(() => {
                    // alert("确定")
                    // 用户点击确定按钮，发送ajax请求，将检查项ID提交到Controller进行处理
                    axios.get("/address/delete.do?id=" + row.id).then((res) => {

                        if (res.data) {
                            //删除成功
                            this.$message({
                                message: '删除成功',
                                type: 'success',
                            });
                            this.findPage()
                        } else {
                            //删除失败
                            this.$message({
                                message: '删除失败',
                                type: 'error',
                            });
                        }
                    }).catch((err) => {
                        // console.log(err)
                        this.showMessage(err);
                    })
                }).catch(() => {
                    // alert("取消")
                    this.$message({
                        message: '操作已取消',
                        type: 'warning',
                    });
                })
            },
            handleClick(tab, event) {
                console.log(tab, event);
                if (this.activeName == 'first') {
                    this.findAll()
                } else {
                    this.findPage()
                }
            },
            mapAddress(){
                map.clearOverlays();
                axios.post("/address/findAll.do?ll="+new Date().getTime()).then((response) => {
                    this.dataList=response.data.data;
                    console.log(this.dataList);
                    for(var i=0; i<this.dataList.length; i++) {//循环地址列表
                        let point =new BMap.Point(this.dataList[i].longitude-0,this.dataList[i].latitude-0);//定位经纬度
                        var marker = new BMap.Marker(point);//创建覆盖物
                        map.addOverlay(marker);//添加覆盖物
                        marker.setAnimation(BMAP_ANIMATION_BOUNCE);//动画

                        var opts = {
                            position: point,    // 指定文本标注所在的地理位置
                            offset: new BMap.Size(10, -30)    //设置文本偏移量
                        };
                        var a  = new BMap.Label(this.dataList[i].address, opts);  // 创建文本标注对象
                        a.setStyle({//设置标注文本的样式
                            color: "black",
                            fontSize: "20px",
                            fontFamily: "微软雅黑"
                        });
                        map.addOverlay(a);//添加标注文本
                    }
                });
            },
            // 弹出添加窗口
            createMap(){

                var map2 = new BMap.Map("smallMap");
                //// 创建地图实例
                var point2 = new BMap.Point(114.309043,30.550317);
                //// 创建点坐标
                map2.centerAndZoom(point2, 12);
                map2.enableScrollWheelZoom();
                window.map2=map2;
            },
            parseAddress(address) {

                var localSearch = new BMap.LocalSearch(map2);
                localSearch.enableAutoViewport(); //允许自动调节窗体大小
                this.searchByStationName(localSearch,address);
            },
            searchByStationName(localSearch,address) {
                map2.clearOverlays();//清空原来的标注
                //var keyword = document.getElementById("address").value;
                localSearch.setSearchCompleteCallback(function (searchResult) {
                    var poi = searchResult.getPoi(0);
                    map2.centerAndZoom(poi.point, 13);
                    var lng=poi.point.lng;
                    window.lng=lng;
                    var lat=poi.point.lat;
                    window.lat=lat;
                    var marker3 = new BMap.Marker(new BMap.Point(poi.point.lng, poi.point.lat));  // 创建标注，为要查询的地方对应的经纬度
                    map2.addOverlay(marker3);
                    marker3.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
                    var optss = {
                        position: poi.point,    // 指定文本标注所在的地理位置
                        offset: new BMap.Size(10, -30)    //设置文本偏移量
                    };
                    var b  = new BMap.Label(address, optss);  // 创建文本标注对象
                    b.setStyle({
                        color: "black",
                        fontSize: "20px",
                        fontFamily: "微软雅黑"
                    });
                    map2.addOverlay(b);
                });
                localSearch.search(address);
            }
        }

    })
</script>
</html>